Add on_missing_context parameter to get_run_logger()#21028
Open
zionts wants to merge 2 commits intoPrefectHQ:mainfrom
Open
Add on_missing_context parameter to get_run_logger()#21028zionts wants to merge 2 commits intoPrefectHQ:mainfrom
zionts wants to merge 2 commits intoPrefectHQ:mainfrom
Conversation
get_run_logger() raises MissingContextError when called outside a flow or task run context. This makes it unusable from threads (e.g. ThreadPoolExecutor workers) where no Prefect context exists, forcing users to monkey-patch the function or wrap every call in try/except. Add an on_missing_context parameter that controls this behavior: - "raise" (default): current behavior, fully backward compatible - "warn": return a standard prefect logger + emit debug message - "ignore": return a standard prefect logger silently This lets users write code that works both inside and outside Prefect run contexts without monkey-patching.
desertaxle
requested changes
Mar 9, 2026
| Get a Prefect logger for the current task run or flow run. | ||
|
|
||
| The logger will be named either `prefect.task_runs` or `prefect.flow_runs`. | ||
| The logger will be named either ``prefect.task_runs`` or ``prefect.flow_runs``. |
Member
There was a problem hiding this comment.
Suggested change
| The logger will be named either ``prefect.task_runs`` or ``prefect.flow_runs``. | |
| The logger will be named either `prefect.task_runs` or `prefect.flow_runs`. |
| Contextual data about the run will be attached to the log records. | ||
|
|
||
| These loggers are connected to the `APILogHandler` by default to send log records to | ||
| These loggers are connected to the ``APILogHandler`` by default to send log records to |
Member
There was a problem hiding this comment.
Suggested change
| These loggers are connected to the ``APILogHandler`` by default to send log records to | |
| These loggers are connected to the `APILogHandler` by default to send log records to |
Comment on lines
+117
to
+122
| on_missing_context: Controls behavior when no active flow or task run context | ||
| exists. ``"raise"`` (default) raises ``MissingContextError`` for backward | ||
| compatibility. ``"warn"`` returns a standard ``prefect`` logger and emits a | ||
| warning. ``"ignore"`` returns a standard ``prefect`` logger silently. Use | ||
| ``"warn"`` or ``"ignore"`` when calling from threads or other contexts where | ||
| a Prefect run context may not be available. |
Member
There was a problem hiding this comment.
Suggested change
| on_missing_context: Controls behavior when no active flow or task run context | |
| exists. ``"raise"`` (default) raises ``MissingContextError`` for backward | |
| compatibility. ``"warn"`` returns a standard ``prefect`` logger and emits a | |
| warning. ``"ignore"`` returns a standard ``prefect`` logger silently. Use | |
| ``"warn"`` or ``"ignore"`` when calling from threads or other contexts where | |
| a Prefect run context may not be available. | |
| on_missing_context: Controls behavior when no active flow or task run context | |
| exists. `"raise"` (default) raises `MissingContextError` for backward | |
| compatibility. `"warn"` returns a standard `prefect` logger and emits a | |
| warning. `"ignore"` returns a standard `prefect` logger silently. Use | |
| `"warn"` or `"ignore"` when calling from threads or other contexts where | |
| a Prefect run context may not be available. |
Comment on lines
+127
to
+128
| MissingContextError: If no context can be found and ``on_missing_context`` is | ||
| ``"raise"`` |
Member
There was a problem hiding this comment.
Suggested change
| MissingContextError: If no context can be found and ``on_missing_context`` is | |
| ``"raise"`` | |
| MissingContextError: If no context can be found and `on_missing_context` is | |
| `"raise"` |
| logger = logging.getLogger("null") | ||
| logger.disabled = True | ||
| elif on_missing_context == "warn": | ||
| logger = get_logger("prefect.run_fallback") |
Member
There was a problem hiding this comment.
I'd feel better about adding this if users had the option to customize this logger name.
| elif on_missing_context == "ignore": | ||
| logger = get_logger("prefect.run_fallback") | ||
| else: | ||
| raise MissingContextError("There is no active flow or task run context.") |
Member
There was a problem hiding this comment.
It'd be good to mention the new on_missing_context kwarg in this exception message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #21027 —
get_run_logger()raisesMissingContextErrorin threaded contexts with no fallback option.When using
ThreadPoolExecutorinside Prefect tasks for parallel processing, worker threads don't inherit the Prefect run context. Any code that callsget_run_logger()in these threads crashes withMissingContextError. The only workaround today is to monkey-patchprefect.get_run_loggerglobally.Changes
New
on_missing_contextparameter onget_run_logger():"raise"(default): current behavior, fully backward compatible"warn": return a standardprefectlogger and emit a debug message"ignore": return a standardprefectlogger silentlyThe fallback logger is a standard
prefect.run_fallbacklogger created via the existingget_logger()function, so it inherits Prefect's logging configuration.Testing
tests/logging/test_on_missing_context.py"raise"behavior is unchanged (backward compat)"warn"returns logger and emits debug message"ignore"returns logger silentlyChecklist
<link to issue>"